<template>
  <div class="movie-list">
    <h1 style="margin:20px 0 30px 0;color: #49c5b6">电影数据后台管理</h1>
    <el-table
      :data="tableData"
      style="width: 100%"
      tooltip-effect="light"
    >
      <el-table-column
        v-for="(column, index) in tableColumns"
        :key="index"
        :label="column.label"
        width="180"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <img v-if="column.prop === 'posterKey'" :src="scope.row[column.prop] | filterContent(column)" width="50" />
          <span v-else style="margin-left: 10px">
            <el-popover trigger="hover" placement="top">
              <p style="padding: 10px;"> {{ column.label }}: {{ scope.row[column.prop] | filterContent(column) }}</p>
              <div slot="reference" class="name-wrapper list">
                <i v-if="column.prop === 'meta' || column.prop === 'pubdate'" class="el-icon-time"></i>
                {{ scope.row[column.prop] | filterContent(column) }}
              </div>
            </el-popover>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[10]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { queryMoviesList, deleteMovie } from '../../utils/api'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

export default {
  name: 'movieList',
  data() {
    return {
      site: 'http://video.fanchao.site/',
      tableColumns: [
        { label: '海报', prop: 'posterKey' },
        { label: '名字', prop: 'title' },
        { label: '上映时间', prop: 'pubdate' },
        { label: '评分', prop: 'rate' },
        { label: '更新时间', prop: 'meta' },
        { label: '豆瓣 ID', prop: 'doubanId' },
        { label: '类型', prop: 'movieTypes' },
        { label: '标签', prop: 'tags' },
        { label: '简要', prop: 'summary' },
      ],
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0
    }
  },
  filters: {
    filterContent (val, column) {
      if (column.prop === 'posterKey') {
        return `http://video.fanchao.site/${val}`
      } else if (column.prop === 'pubdate') {
        return val.map(item => `${moment(item.date).format("YYYY-MM-DD")}(${item.country})`).join('/')
      } else if (column.prop === 'rate') {
        return `${val} 分`
      } else if (column.prop === 'meta') {
        return moment(val.updatedAt).fromNow()
      } else if (column.prop === 'movieTypes') {
        return val.join('/')
      } else if (column.prop === 'tags') {
        return val.join('/')
      }
      return val
    }
  },
  mounted() {
    this._queryMoviesList()
  },
  methods: {
    handleSizeChange(val) {
     this.params.pageSize = val
     this._queryMoviesList()
    },
    handleCurrentChange(val) {
     this.params.pageNum = val
     this._queryMoviesList()
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete({ _id }) {
      this.$confirm('删除数据不可恢复，确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteMovie({ _id,}).then(res => {
          if (res.success) {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.params.pageNum = 1
            this.params.pageSize = 10
            this._queryMoviesList()
          } else {
            this.$alert(res.message, '提示', {
              confirmButtonText: '确定',
              closeOnClickModal: true,
              center: true,
              callback: action => {
              }
            })
          }
        })
      }).catch((err) => {
        console.log(err)
      })
    },
    _queryMoviesList () {
      queryMoviesList(this.params).then(res => {
        this.tableData = res.data
        this.total = res.count
      })
    }
  },
}
</script>
<style lang="scss" scoped>
  .movie-list{
    color: #49c5b6;
    height: 100%;
    padding-bottom: 20px;
    overflow: scroll;
    &::-webkit-scrollbar {
      position: absolute;
      z-index: 100;
      width: 0px;
      margin-left: -10px;
      -webkit-appearance: none;
    }
    &::-webkit-scrollbar-thumb {
      height: 50px;
      background-color: #E6EAEA;
      background-clip: content-box;
      border-color: transparent;
      border-style: solid;
      border-width: 1px 2px;
      border-radius: 5px;
    }
    .slot{
      color: #49c5b6;
    }
    .summary{
      color: #49c5b6;
      line-height: 20px;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical!important;
    }
    .list{
      color: #49c5b6;
      line-height: 20px;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 8;
      -webkit-box-orient: vertical!important;
    }
    .pagination{
      margin: 20px 20px 50px 0;
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
